#include "connectionPool.hpp"
#include "offlinemsgmodel.hpp"

/**
 * @brief 插入离线消息
 *
 * @param userid
 * @param msg
 */
void OfflineMsgModel::insert(int userid, string msg)
{
    char sql[1024] = {0};
    sprintf(sql, "insert into offlinemessage values(%d,'%s')", userid, msg.c_str());

    shared_ptr<MySQL> mysql = ConnectionPool::getConnectionPool()->getMySQL();
    mysql->update(sql);
}
/**
 * @brief 移除离线消息
 *
 * @param userid
 */
void OfflineMsgModel::remove(int userid)
{
    char sql[1024] = {0};
    sprintf(sql, "delete from offlinemessage where userid=%d", userid);

    shared_ptr<MySQL> mysql = ConnectionPool::getConnectionPool()->getMySQL();
    mysql->update(sql);
}
/**
 * @brief 查询离线消息
 *
 * @param userid
 * @return vector<string> 消息数组
 */
vector<string> OfflineMsgModel::query(int userid)
{
    char sql[1024] = {0};
    sprintf(sql, "select message from offlinemessage where userid=%d", userid);

    shared_ptr<MySQL> mysql = ConnectionPool::getConnectionPool()->getMySQL();
    vector<string> ret;
    MYSQL_RES *res = mysql->query(sql);
    if (res != nullptr)
    {
        MYSQL_ROW row;
        while ((row = mysql_fetch_row(res)) != nullptr)
        {
            ret.push_back(row[0]);
        }
        mysql_free_result(res);
    }
    return ret;
}